package com.eastcom.groovy.config

import javax.xml.bind.annotation.XmlAccessType
import javax.xml.bind.annotation.XmlAccessorType
import javax.xml.bind.annotation.XmlElement
import javax.xml.bind.annotation.XmlTransient

import com.eastcom.groovy.context.TaskContext
import com.eastcom.groovy.provider.DynamicBeanRegisterProvider
import com.eastcom.groovy.provider.SpringContextProvider
import com.eastcom.groovy.tools.file.GroovyUtil
import org.apache.commons.logging.*
/**
 * Created by yuanyue on 2014-08-12.
 */
@XmlAccessorType(XmlAccessType.FIELD)
class ExternConfig implements Serializable {
	@XmlElement(name = "table")
	String table
	@XmlElement(name = "config")
	Config config
	@XmlElement(name = "executor")
	Executor executor
}

@XmlAccessorType(XmlAccessType.FIELD)
class Config implements Serializable {
	@XmlElement(name = "config-file")
	String configFile
}

@XmlAccessorType(XmlAccessType.FIELD)
class Executor {
	private static Log logger = LogFactory.getLog(Executor.class)
	@XmlElement(name = "executor-class")
	String executorClass
	@XmlTransient
	GroovyScript groovyScriptInfo
	def init = { rootPath,jobId->
		GroovyScript executorScript = new GroovyScript()
		executorScript.scriptFileRootPath = rootPath
		executorScript.scriptFileName = executorClass
		executorScript.param = jobId
		groovyScriptInfo = executorScript
		//		TaskContext.taskRootPath.put(jobId,rootPath)
		Class<?> clazz = GroovyUtil.loadScriptClass(executorScript)
		logger.debug("script class["+clazz+"]")
		DynamicBeanRegisterProvider beanRegister = SpringContextProvider.getBean("beanRegister")
		beanRegister.addBeanService(jobId+"_"+executorClass,clazz,null)
		GroovyObject classObj = SpringContextProvider.getBean(jobId+"_"+executorClass)
		logger.debug(classObj)
		List rs = classObj.metaClass.respondsTo(classObj,"initJob")
		if(rs != null && !rs.isEmpty()){
			logger.debug("脚本"+classObj)
			logger.debug("开始执行脚本。。。")
			Object o = classObj.invokeMethod("initJob",(String)executorScript.param)
			logger.debug("脚本执行结果："+o)
		}
	}
}
